package com.wl156.adapters;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.activities.messages.AddFeedInternal;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;

public class FeedManagerAdapter extends BaseAdapter {

	private static final String TAG = null;
	SoapObject mSoapObject;
	LayoutInflater mInflater;
	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;
	Context mContext;

	public FeedManagerAdapter(Context context, SoapObject so) {
		mSoapObject = so;
		mInflater = LayoutInflater.from(context);
		mWebServiceClient = new WebServiceClient(context);
		mCertificate = ((MyApp) context.getApplicationContext())
				.getCertificateFromXml();
		mContext = context;
	}

	@Override
	public int getCount() {
		return mSoapObject.getPropertyCount();
	}

	@Override
	public SoapObject getItem(int position) {
		return (SoapObject) mSoapObject.getProperty(position);
	}

	@Override
	public long getItemId(int position) {
		try {
			long id = Long.valueOf((getItem(position).getProperty(0))
					.toString());
			Log.d(TAG, "id: " + id);
			return id;
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = mInflater
				.inflate(R.layout.item_feed_message_internal, null);
		TextView from = (TextView) view.findViewById(R.id.from);
		TextView to = (TextView) view.findViewById(R.id.to);
		TextView type = (TextView) view.findViewById(R.id.type);
		TextView update = (TextView) view.findViewById(R.id.update);
		TextView del = (TextView) view.findViewById(R.id.del);

		setTextViewStringFromProperty(position, from, 1);
		setTextViewStringFromProperty(position, to, 2);
		setTextViewStringFromProperty(position, type, 3);
		update.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(mContext, AddFeedInternal.class);
				i.putExtra("update", true);
				i.putExtra("subscribeID", getItemId(position));
				mContext.startActivity(i);
			}
		});
		del.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new AsyncTask<Void, Void, Object>() {
					ProgressDialog mDialog;

					@Override
					protected void onPostExecute(Object result) {
						mDialog.dismiss();

						if (result instanceof Exception) {
							Exception e = (Exception) result;
							Toast.makeText(mContext, "删除失败，请稍后再试",
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT)
									.show();

						}
					};

					@Override
					protected void onPreExecute() {
						mDialog = ProgressDialog.show(mContext, "", "删除中...");
					};

					@Override
					protected Object doInBackground(Void... params) {
						try {
							return mWebServiceClient.common_deleteSubscribe(
									mCertificate, getItemId(position));
						} catch (IOException e) {
							e.printStackTrace();
							return e;
						} catch (XmlPullParserException e) {
							e.printStackTrace();
							return e;
						}
					}
				}.execute();
			}
		});

		return view;
	}

	protected void setTextViewStringFromProperty(int position, TextView tv,
			int index) {
		try {
			Object prop = getItem(position).getProperty(index);
			String text = "";
			if (prop != null) {
				text = prop.toString();
			}
			if (text.equals("anyType{}")) {
				text = "";
			}
			tv.setText(text);
		} catch (Exception e) {
		}
	}
}
